<?php

namespace App\Http\Logic;

use App\Models\AdminCharityFund;
use App\Models\AdminEducation;
use App\Models\AdminManager;
use App\Models\AdminManagerType;
use App\Models\User;
use App\Models\UsersDonationRecord;
use Illuminate\Support\Facades\Storage;

class ManagerLogic extends CommonLogic
{
    public static function getTypeList()
    {
        $search = [
            ['status','=',1],
        ];
        $data = AdminManagerType::where($search)
        -> orderBy('sort', 'desc')
        -> get()-> toArray();
        return $data;
    }
    public static function getManagerList($params, $page_no = 1, $page_size = 10)
    {
        $offset = ($page_no - 1) * $page_size;
        $search = [
            ['online_status','=',1],
            ['audit_status','=',1],
        ];
        $arr = AdminManager::where($search)
            -> orderBy('sort', 'desc')
            -> limit($page_size)
            -> offset($offset)
            -> get();
        $count = AdminManager::where($search)->count();
        if($arr) {
            $arr = $arr -> toArray();
        }
        foreach ($arr as &$item)
        {
            $item['cover_path'] = $item['cover_path'] ? asset(Storage::url($item['cover_path'])) : "";
        }
        $data = [
            'list'          => $arr,
            'page_no'       => $page_no,
            'page_size'     => $page_size,
            'count'         => $count,
            'more'          => count($arr) >= $page_size
        ];
        return $data;
    }

    public static function getFundList($params, $page_no = 1, $page_size = 10)
    {
        $offset = ($page_no - 1) * $page_size;
        $search = [
            ['online_status','=',1],
            ['audit_status','=',1],
        ];
        $arr = AdminCharityFund::where($search)
            -> orderBy('sort', 'desc')
            -> limit($page_size)
            -> offset($offset)
            -> get();
        $count = AdminCharityFund::where($search)->count();
        if($arr) {
            $arr = $arr -> toArray();
        }
        foreach ($arr as &$item)
        {
            $item['cover_path'] = $item['cover_path'] ? asset(Storage::url($item['cover_path'])) : "";
        }
        $data = [
            'list'          => $arr,
            'page_no'       => $page_no,
            'page_size'     => $page_size,
            'count'         => $count,
            'more'          => count($arr) >= $page_size
        ];
        return $data;
    }

    public static function getEducationList($params, $page_no = 1, $page_size = 10)
    {
        $offset = ($page_no - 1) * $page_size;
        $search = [
            ['online_status','=',1],
            ['audit_status','=',1],
        ];
        $arr = AdminEducation::where($search)
            -> orderBy('sort', 'desc')
            -> limit($page_size)
            -> offset($offset)
            -> get();
        $count = AdminEducation::where($search)->count();
        if($arr) {
            $arr = $arr -> toArray();
        }
        foreach ($arr as &$item)
        {
            $item['cover_path'] = $item['cover_path'] ? asset(Storage::url($item['cover_path'])) : "";
        }
        $data = [
            'list'          => $arr,
            'page_no'       => $page_no,
            'page_size'     => $page_size,
            'count'         => $count,
            'more'          => count($arr) >= $page_size
        ];
        return $data;
    }

    //捐款人数统计
    public static function getCaringIndividuals()
    {
        $search = [
            ['contribution_status','=',1]
        ];
        return User::where($search)
        ->count();
    }

    public static function getProjectCount()
    {
        $search = [
            ['online_status','=',1],
            ['audit_status','=',1],
        ];
        $count1 = AdminCharityFund::where($search)->count();
        $count2 = AdminManager::where($search)->count();
        $count3 = AdminEducation::where($search)->count();
        return $count1 + $count2 + $count3;
    }

    public static function getProjectAmountCount()
    {
        return UsersDonationRecord::sum('amount');
    }


}
